﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Data;

namespace Framework.Common.Common
{
    public static class ExtensionMethods
    {
        #region 数据转换
        public static int ToInt(this object obj)
        {
            return CommonHelper.ObjToInt(obj);
        }

        public static int ToInt(this object obj, int defaultValue)
        {
            if (CommonHelper.ObjToInt(obj) == 0) return defaultValue;
            return CommonHelper.ObjToInt(obj);
        }

        public static string ToStr(this object obj)
        {
            return CommonHelper.ObjToStr(obj);
        }

        public static decimal ToDecimal(this object obj)
        {
            return CommonHelper.ObjToDecimal(obj);
        }


        public static int? ToIntNull(this object obj)
        {
            return CommonHelper.ObjToIntNull(obj);
        }

        public static decimal? ToDecimalNull(this object obj)
        {
            return CommonHelper.ObjToDecimalNull(obj);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ToBool(this object obj)
        {
            return CommonHelper.ObjToBool(obj);
        }
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            if (obj != null)
            {
                return JsonConvert.SerializeObject(obj, new DataTableConverter());
            }
            else
                return "";
        }


        /// md5
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>

        public static string ToMD5(this object obj)
        {
            if (obj == null)
            {
                return "";
            }
            else
            {
                return MD5.Lower32((string)obj);
            }
        }



        /// <summary>
        /// 转换为日期
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime? ToDateNull(this object obj)
        {
            return CommonHelper.ObjToDateNull(obj);
        }

        public static double ToDouble(this decimal value)
        {
            return Convert.ToDouble(value);
        }



        #endregion
    }
}
